/**
 * 
 */
package com.iwords.service.persistence.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.iwords.service.object.HotSentencesEntity;
import com.iwords.service.persistence.HotSentencesPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-4-30    
 * @description         
 */
@Stateless
public class HotSentencesPersistenceImpl implements HotSentencesPersistence {
	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	public void insertHost(HotSentencesEntity hotsentences) {
		String sql = "insert into hotsentences( wordid,sentenceid,rank,createtime,updatetime) " + " values(" + hotsentences.getWordid() + ","
				+ hotsentences.getSentenceid() + "," + hotsentences.getRank() + ",now(),now())";

		getEntityManager().createNativeQuery(sql).executeUpdate();

	}

	public Long getHotCount() {
		Query query = getEntityManager().createQuery("SELECT count(DISTINCT u.sentenceid) from HotSentencesEntity u");
		return (Long) query.getSingleResult();
	}

	public void deleteHost(HotSentencesEntity hotsentences) {
		String sql = " delete from hotsentences  where wordid=" + hotsentences.getWordid() + " and sentenceid=" + hotsentences.getSentenceid();

		getEntityManager().createNativeQuery(sql).executeUpdate();

	}

	@SuppressWarnings("unchecked")
	public List<HotSentencesEntity> getHotListByWordId(Long wordid) {
		Query query = getEntityManager().createQuery("SELECT u from HotSentencesEntity u where u.wordid=?1  ");
		query.setParameter(1, wordid);

		return (List<HotSentencesEntity>) query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Long> getAllHotSentences(int toPage) {
		Query query = getEntityManager().createQuery("SELECT DISTINCT u.sentenceid from HotSentencesEntity u").setFirstResult(toPage * 20).setMaxResults(20);

		return query.getResultList();
	}

}
